package com.easyxu.kubernetes.filebrowser.service.executor;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentList;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class DeploymentQryCmdExe {
    public List<V1Deployment> execute(String namespace, String deployment){
        AppsV1Api api = new AppsV1Api();
        try {
            V1DeploymentList v1DeploymentList = api.listNamespacedDeployment(namespace, null, null, null, null, null, null, null, null, null);

            if(deployment == null){
                return v1DeploymentList.getItems();
            }else{
                return v1DeploymentList.getItems().stream().filter(item->item.getMetadata().getName()
                        .contains(deployment))
                        .collect(Collectors.toList());
            }
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }
}
